# 使用 urllib 来获取百度首页的源码
import urllib.request

# 1.定义一个 url 就是你要访问的网站
url = 'http://www.baidu.com'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
}

# 2.模拟浏览器向服务器发送请求
request = urllib.request.Request(url, headers=headers)

# 1.获取 handler 对象    handler  build_opener  open
handler = urllib.request.HTTPHandler()

# 2.获取build_opener对象
opener = urllib.request.build_opener(handler)

# 调用 open 方法
response = opener.open(request)

content = response.read().decode('utf-8')

print(content)
